package SubjectTree.One;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import Utility.TreeNode;

public class PostorderTraversal {

/**
 * 难度：中等
 * 
 * 145. 二叉树的后序遍历
 * 	给定一个二叉树，返回它的 后序 遍历。
 * 
 * 示例:
 * 	输入: [1,null,2,3]  
 * 	   1
 * 	    \
 * 	     2
 * 	    /
 * 	   3 
 * 	输出: [3,2,1]
 * 
 * 进阶: 递归算法很简单，你可以通过迭代算法完成吗？
 * 
 * */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PostorderTraversal pt = new PostorderTraversal();
//		TreeNode root = new TreeNode(1);
//		root.left = null;
//		root.right = new TreeNode(2);
//		root.right.left = new TreeNode(3);
		TreeNode root = TreeNode.MkTree("[1,null,2,null,null,3,null]");
		
//		TreeNode root = new TreeNode(5);
//		TreeNode left = new TreeNode(4);
//		TreeNode right = new TreeNode(6);
//		root.left = left;
//		root.right = right;
//		left.left = new TreeNode(1);
//		left.right = new TreeNode(2);
//		right.left = new TreeNode(7);
//		right.right = new TreeNode(8);
//		TreeNode root = TreeNode.MkTree("[5,4,6,1,2,7,8]");
		System.out.println(pt.postorderTraversal(root));
	}
	
	public List<Integer> postorderTraversal(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		if(root==null)return list;
		Deque<TreeNode> deque = new LinkedList<>();
		TreeNode node = root;
		//记录前一次的数
		TreeNode prev = null;
		while(!deque.isEmpty()||node!=null) {
			while(node!=null) {
				deque.push(node);
				node = node.left;
			}
			node = deque.pop();
			if(node.right==null||node.right==prev) {
				list.add(node.val);
				prev = node;
				node = null;
			}else {
				deque.push(node);
				node = node.right;
			}
		}
		return list;
    }
	//方法一：递归
	public List<Integer> postorderTraversal1(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		traversal(root,list);
		return list;
    }
	public void traversal(TreeNode root,List<Integer> list) {
		if(root==null) return;
		traversal(root.left,list);
		traversal(root.right,list);
		list.add(root.val);
	}
	//方法二：迭代
	public List<Integer> postorderTraversal2(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        //记录前一次的数
        TreeNode prev = null;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            //如果右腿等于空或者右腿等于前一次的数，则进入记录
            if (root.right == null || root.right == prev) {
                res.add(root.val);
                //记录当前值
                prev = root;
                //把node置于null,进入下一次栈的弹出
                root = null;
            } else {
            	//如果右腿不为空，则把该值再次放入栈中
                stack.push(root);
                root = root.right;
            }
        }
        return res;
    }
	//方法三：Morris 遍历
	 public List<Integer> postorderTraversal3(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }

        TreeNode p1 = root, p2 = null;

        while (p1 != null) {
            p2 = p1.left;
            if (p2 != null) {
                while (p2.right != null && p2.right != p1) {
                    p2 = p2.right;
                }
                if (p2.right == null) {
                    p2.right = p1;
                    p1 = p1.left;
                    continue;
                } else {
                    p2.right = null;
                    addPath(res, p1.left);
                }
            }
            p1 = p1.right;
        }
        addPath(res, root);
        return res;
    }
    public void addPath(List<Integer> res, TreeNode node) {
        int count = 0;
        while (node != null) {
            ++count;
            res.add(node.val);
            node = node.right;
        }
        int left = res.size() - count, right = res.size() - 1;
        while (left < right) {
            int temp = res.get(left);
            res.set(left, res.get(right));
            res.set(right, temp);
            left++;
            right--;
        }
    }
}
